package com.leoman.privateletter.dao;

import com.leoman.common.dao.IBaseJpaRepository;
import com.leoman.privateletter.entity.PrivateLetter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * 私信dao
 * Created by 涂奕恒 on 2016/6/12.
 */
public interface PrivateLetterDao extends IBaseJpaRepository<PrivateLetter> {

    @Query("select a from PrivateLetter a where a.toUser.id = ?1 group by a.user.id")
    List<PrivateLetter> findListGroupByUserId(Long userId);

    @Query("select a from PrivateLetter a where a.user.id = ?1 group by a.toUser.id")
    List<PrivateLetter> findListGroupByToUserId(Long userId);

    @Query("select a from PrivateLetter a where (a.user.id = ?1 and a.toUser.id = ?2) or (a.toUser.id = ?1 and a.user.id = ?2) order by a.id desc")
    List<PrivateLetter> findList(Long userId, Long toUserId);

    @Query("select a from PrivateLetter a where (a.user.id = ?1 and a.toUser.id = ?2) or (a.toUser.id = ?1 and a.user.id = ?2)")
    Page<PrivateLetter> findListSession(Long userId, Long toUserId, Pageable pageable);

    @Query("select a from PrivateLetter a where a.toUser.id = ?1 and a.isRead = 0")
    List<PrivateLetter> findListByUserIdWithNoRead(Long userId);
}
